package com.lbtc.service.Impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.lbtc.dao.CategoryMapper;
import com.lbtc.domian.Category;
import com.lbtc.exception.LbtcMallException;
import com.lbtc.exception.LbtcMallExceptionEnum;
import com.lbtc.request.AddCategoryReq;
import com.lbtc.service.CategoryService;
import com.lbtc.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    CategoryMapper categoryMapper;

    @Override
    public void add(AddCategoryReq addCategoryReq) throws LbtcMallException {
        Category category = new Category();
        //使用工具类来完成类的拷贝，替换原有的set方法
        BeanUtils.copyProperties(addCategoryReq, category);
        Category categoryOld = categoryMapper.selectByName(addCategoryReq.getName());
        if (categoryOld != null) {
            throw new LbtcMallException(LbtcMallExceptionEnum.NAME_EXISTED);
        }
        int count = categoryMapper.insertSelective(category);
        if (count == 0) {
            throw new LbtcMallException(LbtcMallExceptionEnum.CREATE_FAILED);
        }
    }

    @Override
    public void update(Category updateCategory) throws LbtcMallException {
        if (updateCategory.getName() != null) {
            Category categoryOld = categoryMapper.selectByName(updateCategory.getName());
            //判断获取的对象不为空，且获取的对象id与参数中的id不同，则名字冲突，抛出不允许重名异常
            if (categoryOld != null && !categoryOld.getId().equals(updateCategory.getId())) {
                throw new LbtcMallException(LbtcMallExceptionEnum.NAME_EXISTED);
            }
        }
        int count = categoryMapper.updateByPrimaryKeySelective(updateCategory);
        if (count == 0) {
            throw new LbtcMallException(LbtcMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void delete(Integer id) throws LbtcMallException {
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        //查不到记录，无法删除，删除失败
        if (categoryOld == null) {
            throw new LbtcMallException(LbtcMallExceptionEnum.DELETE_FAILED);
        }
        int count = categoryMapper.deleteByPrimaryKey(id);
        if (count == 0) {
            throw new LbtcMallException(LbtcMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public PageInfo listForAdmin(Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize, "type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;
    }

    @Cacheable(value = "LbtcRedisCache",key = "#root.methodName")
    @Override
    public List<CategoryVO> listForCustomer(Integer categoryId) {
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
        recursiveFindCategories(categoryVOList, 0);
        return categoryVOList;
    }

    //递归方法获取目录树
    private void recursiveFindCategories(List<CategoryVO> categoryVOList, Integer parentId) {
        //parentId=0 根级父类
        List<Category> fathers = categoryMapper.selectCategoriesByParentId(parentId);
        if (!CollectionUtils.isEmpty(fathers)) {
            for (Category father : fathers) {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(father, categoryVO);
                categoryVOList.add(categoryVO);//添加一级
                //递归调用，为当前级添加以当前级id为父级的数据
                recursiveFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
            }

        }
        /*//递归获取所有子类别，并组合成为一个“目录树”
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)) {
            for (int i = 0; i < categoryList.size(); i++) {
                Category category = categoryList.get(i);
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVOList.add(categoryVO);
                //给categoryVO设置childCategory属性
                recursiveFindCategories(categoryVO.getChildCategory(), categoryVO.getId());
            }
        }*/
    }

    /**
     * 设置当前id的子菜单
     * @param id
     * @return
     */
    @Cacheable(value = "treeCache#1")
    @Override
    public List<CategoryVO> getMenuList(Integer id) {
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(id);
        List<CategoryVO> rootMenuList = new ArrayList<>();
        if (categoryList != null) {
            for (Category category : categoryList) {
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVO.setChildCategory(getMenuList(categoryVO.getId()));
                rootMenuList.add(categoryVO);
            }
        }
        return rootMenuList;
    }

    @Override
    public List<CategoryVO> listCategoryForCustomer(Integer parentId) {
        ArrayList<CategoryVO> categoryVOList = new ArrayList<>();
        recursiveFindCategories(categoryVOList,parentId);
        return categoryVOList;
    }
}






